package com.tsugun.mediaplayer.view.fragment.subfragment;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.ArtistModel;
import com.tsugun.mediaplayer.presenter.manager.MusicFragmentManager;
import com.tsugun.mediaplayer.presenter.provider.ListProvider;
import com.tsugun.mediaplayer.view.activity.IndexActivity;
import com.tsugun.mediaplayer.view.adapter.ArtistItemAdapter;
import com.tsugun.mediaplayer.view.fragment.ArtistFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * create an instance of this fragment.
 */
public class ArtistListFragment extends SubBaseFragment {

    @BindView(R.id.id_list_recycler)
    RecyclerView mRvArtistList;

    private View mFragment;
    private Context mContext;
    private Unbinder unbinder;
    private IndexActivity indexActivity;
    private MusicFragmentManager mManager;
    private ListProvider mProvider;

    private List<ArtistModel> mResult;
    private RecyclerView.LayoutManager mLayoutManager;
    private boolean isFirst;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isFirst = true;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mFragment = inflater.inflate(R.layout.fragment_list, container, false);
        mContext = container.getContext();
        unbinder = ButterKnife.bind(this, mFragment);

        initData();
        bindData();
        return mFragment;
    }

    /**
     * 初始化数据
     */
    private void initData() {
        indexActivity = (IndexActivity) mContext;
        mManager = indexActivity.getMusicFragmentManager();
        mProvider = indexActivity.getListProvider();
        mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
    }

    /**
     * 绑定数据
     */
    private void bindData() {
        initList();
    }

    /**
     * 添加结果集合
     *
     * @param result list<ArtistModel>
     */
    public void setResult(List<ArtistModel> result) {
        if (mResult == null) {
            mResult = result;
            return;
        }
        mResult = result;
        initList();
    }

    /**
     * 初始化列表
     */
    private void initList() {
        ArtistItemAdapter adapter = new ArtistItemAdapter(mContext, mResult,
                (List<ArtistModel>) mProvider.getFollow());
        adapter.setOnArtistItemClickListener(new ArtistItemAdapter.OnArtistItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startArtistFragment(position);
            }

            @Override
            public void onAddClick(int position, View v) {
                followArtist(position, v);
            }
        });
        mRvArtistList.setAdapter(adapter);
        mRvArtistList.setLayoutManager(mLayoutManager);
        if (!isFirst) {
            mRvArtistList.startLayoutAnimation();
        }
        isFirst = false;
        setSettingTheme(mRvArtistList);
    }

    /**
     * 关注歌手
     */
    private void followArtist(int position, View v) {
        ImageView imageView = (ImageView) v;
        if (mProvider.setFollow(mResult.get(position).getName())) {
            imageView.setImageResource(R.drawable.ic_checked);
            Snackbar.make(v, R.string.str_toast_added, Snackbar.LENGTH_LONG).show();
        } else {
            imageView.setImageResource(R.drawable.ic_add_grey);
            Snackbar.make(v, R.string.str_toast_unAdd, Snackbar.LENGTH_LONG).show();
        }
    }

    private void startArtistFragment(int position) {
        Bundle bundle = new Bundle();
        bundle.putString(ArtistFragment.INTENT_STRING_ARTIST_TITLE, mResult.get(position)
                .getName());
//        musicFragmentManager.addSharedElement(view.findViewById(R.id.id_item_artist_icon));
        ArtistFragment fragment = new ArtistFragment();
        fragment.setArguments(bundle);
        mManager.openFragment(fragment);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
